﻿using System;
using System.Web.UI.WebControls;
using BO.Custom;
using Business;
using Entity;
using FreshCms.BackEnd.ControlsCommon;
using Utilities;

namespace FreshCms.BackEnd.Controls.Module.Category
{
    public partial class CategoryManager : System.Web.UI.UserControl
    {
        #region check login and permission
        InPms chkPms = new InPms();
        public Int32 panelId = 0;
        private void checkLogin()
        {
            // Check and load UC
            if (Request.QueryString["ui"] != null)
                Int32.TryParse(Request.QueryString["ui"], out panelId);
            // check Pms
            if (panelId > 0)
                chkPms = InPmsBusiness.CheckSessionPms(panelId);
            if (chkPms == null)
                Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
            // show Pms in Top Bar
            TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
            ucTopBar.BindData(chkPms);
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!Page.IsPostBack)
            {
                if (chkPms.PmsView == 1)
                {
                    BindStatus();
                    BindParentCate();
                    BindData();
                }
            }
        }


        #region Property
        public int CateType
        {
            get { return _cateType; }
            set { _cateType = value; }
        }
        #endregion
        public int _cateType;

        private void BindData()
        {
            if(_cateType>=0)
            {
                var lst = OutCategoryBusiness.GetOutCategoryList(_cateType, -1);
                if (lst.Count > 0)
                {
                    rpt.DataSource = lst;
                    rpt.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
                    rpt.DataBind();
                }
            }
        }

        void rpt_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                try
                {
                    Label lblStatus = (Label)e.Item.FindControl("lblStatus");
                    DropDownList ddlStatus = (DropDownList)e.Item.FindControl("ddlStatus");
                    Repeater rptChild = (Repeater)e.Item.FindControl("rptChild");
                    OutCategory obj = (OutCategory)e.Item.DataItem;

                    lblStatus.Text = StatusBusiness.GetShortStatusName(obj.CatStatus);

                    ddlStatus.DataSource = StatusBusiness.GetShortListStatus(false);
                    ddlStatus.DataTextField = "Value";
                    ddlStatus.DataValueField = "Id";
                    ddlStatus.DataBind();
                    ddlStatus.SelectedValue = obj.CatStatus.ToString();
                    // Bind color
                    ddlStatus.Items[0].Attributes.Add("style", Constant.StyleDropDisable);
                    ddlStatus.Items[1].Attributes.Add("style", Constant.StyleDropEnable);

                    if (obj.CatStatus == 1)
                        lblStatus.CssClass = "label label-success";
                    else
                        lblStatus.CssClass = "label label-danger";

                    // Bind Data to Child Category
                    var lst = OutCategoryBusiness.GetOutCategoryChildList(obj.CatId, -1);
                    if (lst.Count > 0)
                    {
                        rptChild.DataSource = lst;
                        rptChild.ItemDataBound += new RepeaterItemEventHandler(rptChild_ItemDataBound);
                        rptChild.DataBind();
                    }    
                }
                catch (Exception ex) { }

            }
        }


        void rptChild_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                try
                {
                    Label lblStatus = (Label)e.Item.FindControl("lblStatus");
                    DropDownList ddlStatus = (DropDownList)e.Item.FindControl("ddlStatus");
                    DropDownList ddlParentCate = (DropDownList)e.Item.FindControl("ddlParentCate");
                    OutCategory obj = (OutCategory)e.Item.DataItem;

                    lblStatus.Text = StatusBusiness.GetShortStatusName(obj.CatStatus);

                    ddlStatus.DataSource = StatusBusiness.GetShortListStatus(false);
                    ddlStatus.DataTextField = "Value";
                    ddlStatus.DataValueField = "Id";
                    ddlStatus.DataBind();
                    ddlStatus.SelectedValue = obj.CatStatus.ToString();
                    // Bind color
                    ddlStatus.Items[0].Attributes.Add("style", Constant.StyleDropDisable);
                    ddlStatus.Items[1].Attributes.Add("style", Constant.StyleDropEnable);

                    if (obj.CatStatus == 1)
                        lblStatus.CssClass = "label label-success";
                    else
                        lblStatus.CssClass = "label label-danger";

                    // Bind ParentCate
                    ddlParentCate.DataSource = OutCategoryBusiness.GetOutCategoryList(_cateType, -1);
                    ddlParentCate.DataTextField = "CatName";
                    ddlParentCate.DataValueField = "CatId";
                    ddlParentCate.DataBind();
                    ddlParentCate.SelectedValue = obj.CatParentId.ToString();
                }
                catch (Exception ex) { }

            }
        }

        private void BindStatus()
        {
            ddlStatus.DataSource = StatusBusiness.GetShortListStatus(false);
            ddlStatus.DataTextField = "Value";
            ddlStatus.DataValueField = "Id";
            ddlStatus.DataBind();
            // Bind color
            ddlStatus.Items[0].Attributes.Add("style", Constant.StyleDropDisable);
            ddlStatus.Items[1].Attributes.Add("style", Constant.StyleDropEnable);
        }

        private void BindParentCate()
        {
            if(_cateType>=0)
            {
                ddlParentCate.DataSource = OutCategoryBusiness.GetOutCategoryList(_cateType, 1);
                ddlParentCate.DataTextField = "CatName";
                ddlParentCate.DataValueField = "CatId";
                ddlParentCate.DataBind();
            }
        }
    }
}